/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace Octopus {
	public class Albumlist : Object, IMedialibQuery {
		private Gee.List<Album> _albumlist;

		public Albumlist () {
			Object ();
		}

		construct {
			_albumlist = new Gee.ArrayList<Album> ();
		}

		public void load () {
			var intersection = new Xmms.Collection (Xmms.CollectionType.INTERSECTION);
			var universe = Xmms.Collection.universe ();
			var has_album_artist = new Xmms.Collection (Xmms.CollectionType.HAS);
			has_album_artist.attribute_set ("field", "artist");
			has_album_artist.attribute_set ("field", "album");
			has_album_artist.add_operand (universe);
			intersection.add_operand (has_album_artist);

			string query = Global.get ().albumlist_load_query;
			if (query != null && query != "") {
				Xmms.Collection filter;
				GLib.debug ("query:  %s", query);
				Xmms.Collection.parse (query, out filter);
				intersection.add_operand (filter);
			}

			var order = new Xmms.Value.from_list ();
			order.list_append (new Xmms.Value.from_string ("performer"));
			order.list_append (new Xmms.Value.from_string ("artist"));
			order.list_append (new Xmms.Value.from_string ("album"));
			order.list_append (new Xmms.Value.from_string ("picture_front"));
			order.list_append (new Xmms.Value.from_string ("partofset"));
			var fetch = new Xmms.Value.from_list ();
			fetch.list_append (new Xmms.Value.from_string ("artist"));
			fetch.list_append (new Xmms.Value.from_string ("performer")); //used for compilations
			fetch.list_append (new Xmms.Value.from_string ("album"));
			fetch.list_append (new Xmms.Value.from_string ("picture_front"));
			fetch.list_append (new Xmms.Value.from_string ("partofset")); //used for multi-cds

			var group = new Xmms.Value.from_list ();
			group.list_append (new Xmms.Value.from_string ("album"));
			group.list_append (new Xmms.Value.from_string ("picture_front"));
			group.list_append (new Xmms.Value.from_string ("partofset"));

			Global.get ().get_xmms_client ().get_xmms ().coll_query_infos (
			  intersection, //Collection coll
			  order,        //Xmms.Value order
			  0,            //uint limit_start = 0
			  0,            //uint limit_len = 0
			  fetch,        //Xmms.Value? fetch = null
			  group         //Xmms.Value? group = null
			  ).notifier_set (on_coll_query_albumlist);
		}

		private bool on_coll_query_albumlist(Xmms.Value values) {
			unowned Xmms.ListIter iter;
			_albumlist.clear ();

			if (values.is_error()) {
				unowned string error = "unknown";
				values.get_error(out error);
				GLib.warning ("warning, failed to query (%s)", error);
				return true;
			}

			values.get_list_iter(out iter);
			if (!iter.valid()) loaded (); //empty iterator; no query result

			for (iter.first(); iter.valid(); iter.next()) {
				unowned Xmms.Value entry;

				iter.entry(out entry);
				Album e = new Album (entry);
				_albumlist.add (e);
			}
			loaded ();
			return true;
		}

		public void request_data () {
			foreach (var i in _albumlist) {
				add (i);  //emit signal
			}
			add_finished ();
		}

		public int get_size () {
			return this._albumlist.size;
		}

		public int search_string_position (string search) {
			int i;
			for (i = 0; (i < _albumlist.size); i++) {
				if (search.down () <= _albumlist[i].get_compare_string ().down ()) break;
			}
			//no match retrun -1
			if (i == _albumlist.size) i = -1; 

			return i;
		}
	}
}
